﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Honestysoft.Financing.Model;
using Honestysoft.BaseObject;
using System.Data.Common;

namespace Honestysoft.Financing.WebUI.Module.Reports.JSON
{
    /// <summary>
    /// SaveOther 的摘要说明
    /// </summary>
    public class SaveOther : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string id = context.Request.Params["ID"];
            string dataString = context.Request.Params["Data"];
            if (string.IsNullOrEmpty(dataString))
            {
                context.Response.Write("0");
            }
            else
            {
                string[] arrItem = dataString.Split(',');
                List<Bu_WeekReport_Other> lsOther = new List<Bu_WeekReport_Other>();
                Bu_WeekReport_Other item = null;
                string[] arrInv = null;
                int sortNo = 0;
                foreach (string s in arrItem)
                {
                    arrInv = s.Split('|');
                    item = new Bu_WeekReport_Other();
                    item.ID = Honestysoft.BaseLibrary.Common.Guid.GetGuid();
                    item.WeekReportID = id;
                    item.SortNo = sortNo++;
                    item.Description = arrInv[1];
                    lsOther.Add(item);
                }
                BaseBusinessLogic<Bu_WeekReport_Other> bll = new BaseBusinessLogic<Bu_WeekReport_Other>();
                QueryParameters query = new QueryParameters();
                query.Add("WeekReportID", id, QueryParameters.OperatorType.Equal);
                DbTransaction transaction = Honestysoft.BaseLibrary.Data.DatabaseProvider.GetDbTransaction();
                DbConnection connection = transaction.Connection;
                try
                {
                    bll.DeleteByWhere(query, transaction);
                    bll.CreateEntitys(lsOther, transaction);
                    transaction.Commit();
                    context.Response.Write("0");
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    Honestysoft.BaseLibrary.ExceptionHandling.ExceptionHelper.ExceptionDistribute(ex, false);
                    context.Response.Write("保存其他事项时发生错误：" + context.Server.HtmlEncode(ex.Message));
                }
                finally
                {
                    connection.Close();
                }                
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}